package com.example.moshudriver.fragment;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.BeeFramework.Utils.Utils;
import com.BeeFramework.model.BusinessResponse;
import com.sgr.dykdriver.R;
import com.example.moshudriver.activity.SlidingActivity;
import com.example.moshudriver.model.UserBalanceModel;
import com.example.moshudriver.protocol.ApiInterface;
import com.example.moshudriver.tools.MessageConstant;
import com.example.moshudriver.tools.O2OMobileAppConst;
import com.example.moshudriver.tools.SESSION;
import com.external.androidquery.callback.AjaxStatus;
import com.external.eventbus.EventBus;
import com.external.maxwin.view.IXListViewListener;
import com.external.maxwin.view.XListView;
import com.nostra13.universalimageloader.core.ImageLoader;
//展示余额界面，暂不用
public class ShowMyBalanceFragment extends Fragment implements
		IXListViewListener, BusinessResponse {
	private ImageView iv_home_menu;
	private int mUserId;
	private UserBalanceModel mUserBalance;
	protected ImageLoader mImageLoader = ImageLoader.getInstance();
	int count = 2;
	private TextView my_balance, top_view_title;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		SharedPreferences sp = getActivity().getSharedPreferences(
				O2OMobileAppConst.USERINFO, 0);
		mUserId = sp.getInt("uid", 0);
		View view = inflater.inflate(R.layout.show_my_balance, null);

		initView(view);
		// my_balance = (TextView) view.findViewById(R.id.tv_my_balance);
		// iv_home_menu = (ImageView) view.findViewById(R.id.home_menu);
		// view.setOnClickListener(null);
		// XListView lv_my_business = (XListView) view
		// .findViewById(R.id.lv_my_business);
		// lv_my_business.setXListViewListener(this, 0);
		// lv_my_business.setPullLoadEnable(true);
		// lv_my_business.setRefreshTime();
		// // 模拟数据
		// MyAdapter adapter = new MyAdapter();
		// lv_my_business.setAdapter(adapter);
		//
		// mUserBalance = new UserBalanceModel(getActivity());
		// mUserBalance.addResponseListener(this);
		// mUserBalance.getProfile(mUserId);
		//
		// if (mUserId == SESSION.getInstance().uid) {
		// mUserBalance.get();
		// }
		// if (!EventBus.getDefault().isregister(this)) {
		// EventBus.getDefault().register(this);
		// }
		return view;
	}

	private void initView(View view) {
		// TODO Auto-generated method stub

		top_view_title = (TextView) view.findViewById(R.id.top_view_title);
		top_view_title.setText(R.string.my_purse);
		my_balance = (TextView) view.findViewById(R.id.tv_my_balance);
		iv_home_menu = (ImageView) view.findViewById(R.id.top_view_back);
		view.setOnClickListener(null);
		XListView lv_my_business = (XListView) view
				.findViewById(R.id.lv_my_business);
		lv_my_business.setXListViewListener(this, 0);
		lv_my_business.setPullLoadEnable(true);
		lv_my_business.setRefreshTime();
		// 模拟数据
		MyAdapter adapter = new MyAdapter();
		lv_my_business.setAdapter(adapter);

		mUserBalance = new UserBalanceModel(getActivity());
		mUserBalance.addResponseListener(this);
		mUserBalance.getProfile(mUserId);

		if (mUserId == SESSION.getInstance().uid) {
			mUserBalance.get();
		}
		if (!EventBus.getDefault().isregister(this)) {
			EventBus.getDefault().register(this);
		}
	}

	public class MyAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return count;
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			// View view = View.inflate(getActivity(),
			// R.layout.show_my_balance_detail, null);
			View view = LayoutInflater.from(getActivity()).inflate(
					R.layout.show_my_balance_detail, null);
			TextView tv_time = (TextView) view.findViewById(R.id.tv_time);
			TextView tv_business = (TextView) view
					.findViewById(R.id.tv_business);
			TextView tv_money = (TextView) view.findViewById(R.id.tv_money);

			return view;
		}

	}

	@Override
	public void onResume() {
		if (mUserId == SESSION.getInstance().uid) {
			mUserBalance.get();
		}
		super.onResume();
	}

	@Override
	public void onDestroyView() {
		if (EventBus.getDefault().isregister(this)) {
			EventBus.getDefault().unregister(this);
		}
		super.onDestroyView();
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		iv_home_menu.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				((SlidingActivity) getActivity()).showLeft();
			}
		});
	}

	public void onEvent(Object event) {
		Message message = (Message) event;
		if (message.what == MessageConstant.SHOW_MY_BALANCE) {
			mUserBalance.get();
			// mUserModel.getInviteCode();
		}
	}

	@Override
	public void onRefresh(int id) {
		// TODO Auto-generated method stub
		count = 2;
	}

	@Override
	public void onLoadMore(int id) {
		// TODO Auto-generated method stub
		count++;
	}

	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException {
		if (url.endsWith(ApiInterface.USER_BALANCE)) {
			Log.i("余额", "yue");
			my_balance.setVisibility(View.VISIBLE);
			my_balance.setText(Utils.formatBalance(mUserBalance.publicBalance)
					+ getString(R.string.yuan));
		}

	}

}
